﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using BusinessLogicLayer;
using Entities;
using System.Globalization;
using System.Collections;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
using QLNhaSach.Class;

namespace QLNhaSach.Modules
{
    public partial class TraCuuSach : UserControl
    {
        BindingList<TraCuuSachOBJ> gridDataList = new BindingList<TraCuuSachOBJ>();
        public TraCuuSach()
        {
            InitializeComponent();
            initComboboxTheLoai();
            initComboboxGia();
        }
        public void initComboboxTheLoai()
        {
            RepositoryItemImageComboBox properties = imageComboBoxEdit1.Properties;
            properties.Items.BeginUpdate();
            try
            {
                TheLoaiBO tlbo = new TheLoaiBO();
                DataTable dt = tlbo.GetDanhSachTheLoai();
                properties.Items.Add(new ImageComboBoxItem("Chọn thể loại", -1, -1));
                foreach (DataRow row in dt.Rows) // Loop over the rows.
                {
                    //Initialize each item with the display text, value and image index 
                    String tenTheLoai = (String)row["TL_TENTHELOAI"];
                    String maTheLoai = (String)(row["TL_MATHELOAI"].ToString());
                    properties.Items.Add(new ImageComboBoxItem(tenTheLoai, maTheLoai, 0));
                }
                
                
            }
            finally
            {
                properties.Items.EndUpdate();
            }
            //Select the second item  
            imageComboBoxEdit1.SelectedIndex = 0;
        }
        public void initComboboxGia(){
            RepositoryItemImageComboBox properties = imageComboBoxEdit2.Properties;
            properties.Items.BeginUpdate();
            try
            {
                //Initialize each item with the display text, value and image index 
                properties.Items.Add(new ImageComboBoxItem("Chọn giá", -1, -1));
                properties.Items.Add(new ImageComboBoxItem("Nhỏ hơn 100.00VNĐ", 0, 0));
                properties.Items.Add(new ImageComboBoxItem("100.000 - 300.000", 1, 1));
                properties.Items.Add(new ImageComboBoxItem("300.000 - 500.000", 2, 2));
                properties.Items.Add(new ImageComboBoxItem("500.000 - 1.000.000", 3, 3));
                properties.Items.Add(new ImageComboBoxItem("Trên 1.000.000", 4, 4));
            }
            finally
            {
                properties.Items.EndUpdate();
            }
            //Select the second item  
            imageComboBoxEdit2.SelectedIndex = 0;
        }
        private void simpleButton5_Click(object sender, EventArgs e)
        {
            gridDataList.Clear();
            Sach s = new Sach();
            String maSach = textEdit1.Text;
            if (maSach == "")
            {
                maSach = "0";
                s.MaSach = int.Parse(maSach);
            }else{
                s.MaSach = int.Parse(maSach);
            }
            s.TenSach = textEdit3.Text;
            s.MaTheLoai = int.Parse(imageComboBoxEdit1.EditValue.ToString());
            s.DonGia = double.Parse(imageComboBoxEdit2.EditValue.ToString());
            String tenTacGia = textEdit4.Text;
            SachBO sbo = new SachBO();
            DataTable dt = sbo.TraCuuSach(s, tenTacGia);
            int i = 1;
            foreach (DataRow row in dt.Rows)
            {
                gridDataList.Add(new TraCuuSachOBJ(i++.ToString(), row["S_MASACH"].ToString(), (String)row["S_TENSACH"], (String)row["TL_TENTHELOAI"], (String)row["TG_TENTACGIA"], row["S_DONGIA"].ToString(), row["S_SOLUONGTON"].ToString()));
            }
            gridControl1.DataSource = gridDataList;
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            textEdit1.Text = "";
            textEdit3.Text = "";
            textEdit4.Text = "";
            gridDataList.Clear();
        }

        private void simpleButton7_Click(object sender, EventArgs e)
        {
            Environment.Exit(1);
        }

        private void textEdit1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar))
            {
                MessageBox.Show("Chỉ nhập kí tự số");
                e.Handled = true;
            }
        }
    }
}
